/**
 * 门户扩展模块，提供门户网站的动态及静态访问
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","ajaxServ","ckeditor-jquery","nivo","commonServ","loginSignModule","twbsPagination"],
		function($,sysmanMain,ajaxServ,ckeditor,nivo,commonServ,loginSign,twbsPagination) {
			
		'use strict';
		
		var _rootpath,_moduleConfig,curpageParam,_count = 0;
		var _pageRows = 20,_page=0;
		
		//查询条件临时变量
		var _oldConditionStr,_newConditionStr;
		/**
		 * 当前点击文档类型
		 */
		var clickedTypeCode;
		
		
		/**
		 * 查询条件对象
		 */
		var _searchEl;
		
		
		/**
		 * 当前页
		 */
		var currentPage;
		
		
		
		var public_method ={
			
			
			/**
			 * 初始化内容页
			 * @param _options.id
			 * @param _options.type
			 * @param _options.lobid
			 */
			initContentPage:function(_options){
				var _param = window.location.search
				var _array = _param.split('=');
				if(_array.length=2){
					_options.id = _array[1]	
				}
				var org_name = commonServ.getHostpital()
			    var _orgEl = $("span.hospital-name")
			    $(_orgEl).text(org_name)
				private_method.initContentPage(_options);
			},
			
			/**
			 * 初始化内容列表页
			 * 
			 */
			initContentList:function(_options){
				//获取页面参数
				var _param = window.location.search
				
				var _jsonParam = {}
				if(_param){
					_param = _param.substring(1,_param.length);
					//分解参数名值对
					var _array = _param.split('&');
					if(_array &&  _array.length>0){
						for(var i=0;i<_array.length;i++){
							
							var _nv = _array[i].split('=');							
							
							if(_nv[0]){
								if(_nv[1]){
									_jsonParam[_nv[0]]=_nv[1];
								}else{
									_jsonParam[_nv[0]]=null;
								}
							}
						}
					}
				}
				
				
				
				
				var org_name = commonServ.getHostpital()
				
			    $("span.hospital-name").text(org_name);
			    
			    $('form.condition-form').form({
			    	onSubmit:private_method.doSearch
			    })
			    private_method.initContentList(_jsonParam);
			}
			
			
			
		}
		
		var private_method ={
			
			/**
			 * @param _options.id
			 * @param _options.type
			 * @param _options.lobid
			 */
			initContentPage:function(_options){
				
				if(window.console){
					console.log('initContentPage loaded and starting...')
				}
				var _defaultConfig = {
				}
				
				_defaultConfig =$.extend({},_defaultConfig,curpageParam,_options)
				if(_options.id){
					//查询页面内容
					var _url = '/portal/getContentOne';
					
					var _param = {
						url:_url,
						data:{docid:_options.id},
						success:function(data){
							if(data){								
									
								if(data.name){
									//文档标题
									var contentHeaderEl = $('div.page-title')
									
									if(contentHeaderEl.length==1){
										$(contentHeaderEl).html(data.name)
									}
								}
								
								if(data.typeName){
									//文档类型
									var contentHeaderEl = $('span.doctype')
									
									if(contentHeaderEl.length==1){
										$(contentHeaderEl).html('文档类别:'+data.typeName)
									}
								}															
									
								//发布人员
								var public_dept_user
								var contentHeaderEl = $('span.public_dept_user')
								
								if(contentHeaderEl.length==1){
									if(data.public_dept_name){
										public_dept_user = '发布人: ' +data.public_dept_name
										if(data.public_user_name){
											public_dept_user += ' ' + data.public_user_name;
										}
									}else{
										if(data.public_user_name){
											public_dept_user = '发布人: ' +data.public_user_name
										}
									}
									if(public_dept_user)
									$(contentHeaderEl).html(public_dept_user)
								}
								
								
								if(data.public_date){										
									
									//发布时间										
									var contentEl = $('span.public_date')
									if(contentEl.length==1){
										$(contentEl).html(data.public_date)
									}
								}
								
								
								if(data.lob_contents){										
									
									//文档内容										
									var contentEl = $('div.content-text')
									if(contentEl.length==1){
										$(contentEl).html(data.lob_contents)
									}
								}
								
							}
						}						
					}
					
					$.ajax(_param)
					//附加页面内容
					
					//显示页面内容
				}
				
			},
			
			/**
			 * @param _options.typename 分类名称
			 */
			initContentList:function(_options){
				if(window.console){
					console.log('initContentList loaded and starting...')
				}
				var _defaultConfig = {
				}
				
				$.fn.twbsPagination.defaults = 
					$.extend(
						$.fn.twbsPagination.defaults,
						{
							first: '首页',
					        prev: '上页',
					        next: '下页',
					        last: '末页'
					    })
				_defaultConfig =$.extend({},_defaultConfig,curpageParam,_options)
				
				
				var _data
				
				if(_options.typeCode){
					//查询页面内容
					var _url = '/portal/getDoctypeListDomain';
					var _typecode = _options.typeCode
					
								
					if(_typecode.indexOf(',')>0){
						_data = {bizcode_in:_options.typeCode}
					}else{
						_data = {bizcode:_options.typeCode}
					}
					
					//判断分类查询方式
					if(_options.listType && _options.listType==='parent'){
						_url = '/portal/getDoctypeSubListDomain';
					}else{
						_data = {condition:_data};
					}
					
					var _param = {
						url:_url,
						data:_data,
						type:'post',
						success:function(data){
							if(data){								
								
								//设置文档分类内容
								if(data.total){
									
									var _row = data.rows[0];
									clickedTypeCode = _row.bizcode;
									//只有一项分类
									if(data.total==1){
										
								   		$('div.main-item span.red').text(data.rows[0].name);
								   		
								    }else{
										//文档标题
										var typeGroupEl = $('div.sidebar')
										
										if(typeGroupEl.length==1){
											var _rows = data.rows
											var _count = data.total,_html=''
											for(var i=0;i<_count;i++){
												_html +='<a href="javascript:void(0);" class="list-group-item" typeCode="' +data.rows[i].bizcode+'">'
													+'<h4 class="content-type"><span class="red">'
													+data.rows[i].name +'</span></h4></a>'
											}
											$(typeGroupEl).append(_html)
										}									
										
								    }
								    //绑定文档类型点击事件
									$('a.list-group-item').unbind('click');
									$('a.list-group-item').bind('click',private_method.typeClick);
									
									//绑定查询事件
									$('a.search-btn').unbind('click');
									$('a.search-btn').bind('click',private_method.doSearch);
									
									$(document).keypress(function(e) {  
									    // 回车键事件  
								       if(e.which == 13) {
								           private_method.doSearch();  
								       }  
								   });
								   
								   private_method.typeClick();
								   
								}								
							}
						}						
					}					
					$.ajax(_param)					
				}
			},
			
			/**
			 * @param 
			 */
			typeClick:function(e){
				if(e && e.currentTarget){
					var _el = e.currentTarget
					clickedTypeCode = $(_el).attr('typeCode')
				}
				if(clickedTypeCode){
					
					var _url = '/portal/getTypeListDomain';
					var _data = {typeCode:clickedTypeCode}
					var _param = {
						url:_url,
						data:{condition:_data},
						type:'post',
						success:function(data){							
							
							var _param = {
								total:data.total
							}
							//生成pagination							
							private_method.initPagination(_param);
							private_method.doSearch();
							
							
						}
					}
					$.ajax(_param);
				}
			},
			
			/**
			 * 初始化分页
			 * @param _options.page
			 * @param _options.total
			 */
			initPagination:function(_options){
				if(_options.total && _options.total >0){
					var _param = {
						totalPages:Math.ceil(parseInt(_options.total)/_pageRows),
						visiblePages:5,
						onPageClick:function(event,page){
							currentPage = page;
					  		var _param={
					  			page:page,
					  			condition:{typeCode:clickedTypeCode}					  			
					  		}
							private_method.pageClick(_param);
						}
					}
					
					var _el = $('div.pagination').twbsPagination(_param)
				}
				
			}
			,
			/**
			 * 执行查询
			 */
			doSearch:function(){
				var _condition = private_method.getCondition()		
				
				private_method.pageClick(_condition)				
			},
			getSearchEl:function(){
				if(!_searchEl){
					_searchEl = $('form.condition-form #searchParam')			
				}
				
				return _searchEl
				
			},
			/**
			 * 获取查询参数
			 */
			getCondition:function(){
				var retVal = {}
				if(clickedTypeCode){
					retVal = {condition:{typeCode:clickedTypeCode}}
					var _condition = private_method.getSearchEl().val();
					if(_condition){
						var _fuzzy ={														
							fuzzy:{name_like:_condition,
							public_dept_name_like:_condition,
							public_user_name_like:_condition,
							public_date_like:_condition}
						}
						$.extend(retVal,_fuzzy)
					}					
				}
				return retVal;
			},
			/**
			 * 点击文档类型事件
			 */
			pageClick:function(_options){
				//执行查询
				var _param = {
					page:currentPage,
					rows:_pageRows
				}
				var _url = '/portal/getTypeListDomain';
				
				var _data = $.extend(_param,private_method.getCondition(),_options)			
				
				
				$.ajax({
					url:_url,
					data:_data,
					type:'post',
					success:private_method.refreshPage
				})
				
			},
			
			refreshPage:function(data){
				//清除原有数据
				
				var rePage
				
				_newConditionStr =private_method.getSearchEl().val()				
					
				//查询条件变化
				if(!_oldConditionStr || _oldConditionStr!=_newConditionStr){
					rePage = true;
					_oldConditionStr = _newConditionStr;					
				}else{
					rePage = false;
				}
				
				var _el = $('div.table-responsive tr.data-row')
				$(_el).remove();
				if(rePage){
					private_method.initPagination({total:data.total})
				}
				
				
				if(data && data.rows && data.rows.length>0){
					var _el = $('div.table-responsive tbody');
					var _rows = data.rows;
					var _html='';
					var _count = data.rows.length
					for(var i=0;i<_count;i++){
						var _tmpRow = {
							public_date:''
						}
						var _row = _rows[i]
						var _date ='', _dept = '',_user ='';
						
						if(!("public_date" in _row) || (!_row.public_date)){
							_date = _row.createTime
						}else{
							_date = _row.public_date
						}
						
						if(_row.public_dept_name){
							_dept = _row.public_dept_name
						}
						
						if(_row.public_user_name){
							_user = _row.public_user_name
						}
						_html += '<tr class="data-row">'+		                  
		                  '<th class="col-md-6"><a href="/portal/contentPage?docid='+_row.id+'" target="_blank">'+_row.name+'</a></th>'+
		                  '<th class="col-md-2 placeholder">'+_dept+'</th>'+
		                  '<th class="col-md-2 placeholder">'+_user+'</th>'+
		                  '<th class="col-md-2 placeholder">'+_date+'</th>'+
		                  '</tr>';					     
					}
					
					$(_el).append(_html);
				}
				//根据
			},
			
			
			getRootPath:function(){
				var patharr = window.location.pathname.split('/')
        		var rootPath = patharr[1]
        		return rootPath
			},		
			
			//绑定页面点击链接
			bindContentPageClick:function(){
				var _pages = $('a.doc-file');
				$(_pages).unbind('click');
				$(_pages).bind('click',private_method.openPage);
			},
			openPage:function(e){
				var _url = '/portal/contentPage'
				
				if(e){
					var _target = e.currentTarget
					var _id = $(_target).attr("docid")
					var _type = $(_target).attr("docid")
					var _lobid = $(_target).attr("lobid")
					curpageParam = {id:_id,type:_type,lobid:_lobid}
					_url = _url + '?docid='+_id
					window.open(_url,'_blank')
				}
			}
			
			
		}
			
		
		return public_method;
			
})